//
//  APIParameters.swift
//  MyWeatherApp
//
//  Created by Shengbing Li (QXU7859) on 2021/2/27.
//  Copyright © 2021 shanghai. All rights reserved.
//

import Alamofire

protocol APIParameters {
    func toParameters() -> Parameters?
}

extension APIParameters {
    func toParameters() -> Parameters? {
        let paramDict = modelToDict(model: self as AnyObject)
        return paramDict as? Parameters
    }
    
    private func modelToDict(model: AnyObject) -> NSDictionary {
        let redic = NSMutableDictionary()
        let mirror: Mirror = Mirror(reflecting: model)
        for p in mirror.children {
            if (p.label! != "") {
                redic.setValue("\(p.value)", forKey: p.label!)
            }
        }
        
        return redic
    }
}
